<script setup lang="ts">
import { ref } from 'vue';

import { message } from 'ant-design-vue';
import dayjs from 'dayjs';

// 接口定义
interface Customer {
  id: string;
  name: string;
  petName: string;
  phone: string;
  memberLevel: 'gold' | 'normal';
  memberCard: string;
  balance: number;
  type: 'registered' | 'walkin';
}

interface Props {
  visible: boolean;
  amount: number;
  customer: Customer | null;
}

interface Emits {
  (e: 'update:visible',value: boolean): void;
  (e: 'success'): void;
}

const props = defineProps<Props>();
const emit = defineEmits<Emits>();

// 响应式数据
const operator = ref<string>('');
const description = ref<string>('');
const dueDate = ref<any>(dayjs().add(7, 'day'));
const enableReminder = ref<boolean>(true);

// 方法
const handleCancel = () => {
  emit('update:visible', false);
  resetForm();
};

const handleConfirm = () => {
  if (!props.customer) {
    message.warning('顾客信息不存在');
    return;
  }

  if (!operator.value) {
    message.warning('请选择操作人员');
    return;
  }

  if (!description.value.trim()) {
    message.warning('请填写挂账说明');
    return;
  }

  if (!dueDate.value) {
    message.warning('请选择计划到账日期');
    return;
  }

  // 模拟挂账成功
  message.success('挂账成功！');
  emit('success');
  emit('update:visible', false);
  resetForm();
};

const resetForm = () => {
  operator.value = '';
  description.value = '';
  dueDate.value = dayjs().add(7, 'day');
  enableReminder.value = true;
};
</script>

<template>
  <a-modal
    :visible="visible"
    title="挂账"
    width="500px"
    @cancel="handleCancel"
    :footer="null"
  >
    <div class="credit-modal">
      <!-- 挂账信息 -->
      <div class="credit-info">
        <div class="info-item">
          <span class="label">挂账金额:</span>
          <span class="value amount">¥{{ amount.toFixed(2) }}</span>
        </div>
        <div class="info-item">
          <span class="label">顾客姓名:</span>
          <span class="value">{{ customer?.name }}</span>
        </div>
        <div class="info-item">
          <span class="label">联系方式:</span>
          <span class="value">{{ customer?.phone }}</span>
        </div>
      </div>

      <!-- 挂账表单 -->
      <div class="credit-form">
        <div class="form-item">
          <span class="label">操作人员:</span>
          <a-select
            v-model:value="operator"
            style="width: 200px"
            placeholder="请选择挂账人员"
          >
            <a-select-option value="曾前台">曾前台</a-select-option>
            <a-select-option value="李销售">李销售</a-select-option>
            <a-select-option value="王销售">王销售</a-select-option>
          </a-select>
          <span class="required">*</span>
        </div>

        <div class="form-item">
          <span class="label">挂账说明:</span>
          <a-textarea
            v-model:value="description"
            :rows="4"
            style="width: 200px"
            placeholder="请输入挂账说明"
          />
        </div>

        <div class="form-item">
          <span class="label">计划到账:</span>
          <a-date-picker
            v-model:value="dueDate"
            style="width: 200px"
            placeholder="请选择计划到账日期"
          />
        </div>

        <div class="form-item">
          <span class="label"></span>
          <div class="checkbox-group">
            <a-checkbox v-model:checked="enableReminder"> 到期提醒 </a-checkbox>
            <div class="reminder-hint">*系统将在挂账到期当天为您发送提醒</div>
          </div>
        </div>
      </div>

      <!-- 操作按钮 -->
      <div class="modal-actions">
        <a-button @click="handleCancel">取消</a-button>
        <a-button type="primary" @click="handleConfirm"> 确定挂账 </a-button>
      </div>
    </div>
  </a-modal>
</template>

<style scoped>
.credit-modal {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.credit-info {
  padding: 16px;
  background: #f8f9fa;
  border: 1px solid #e8e8e8;
  border-radius: 6px;
}

.info-item {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
}

.info-item:last-child {
  margin-bottom: 0;
}

.info-item .label {
  width: 100px;
  font-size: 14px;
  color: #666;
}

.info-item .value {
  font-weight: bold;
  color: #333;
}

.info-item .amount {
  font-size: 16px;
  color: #ff4d4f;
}

.credit-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.form-item {
  display: flex;
  gap: 12px;
  align-items: flex-start;
}

.form-item .label {
  width: 100px;
  font-size: 14px;
  line-height: 32px;
  color: #666;
}

.form-item .required {
  font-size: 14px;
  line-height: 32px;
  color: #ff4d4f;
}

.checkbox-group {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.reminder-hint {
  margin-left: 24px;
  font-size: 12px;
  color: #999;
}

.modal-actions {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  padding-top: 16px;
  border-top: 1px solid #e8e8e8;
}
</style>
